import 'package:flutter/material.dart';

extension TextStyleExtention on TextStyle {
  double displayWidth(String? text) {
    if (text?.isEmpty ?? true) return 0;
    final textPainter = TextPainter(textDirection: TextDirection.ltr)
      ..text = TextSpan(text: text, style: this)
      ..layout();
    return textPainter.width;
  }

  double displayHeight(String text) {
    if (text.isEmpty) return 0;
    final textPainter = TextPainter(textDirection: TextDirection.ltr)
      ..text = TextSpan(text: text, style: this)
      ..layout();
    return textPainter.height;
  }
}

List<Widget> filterWidgets(
    BuildContext context, List<WidgetWrapper> wrappers, Widget divider) {
  final visibleWidgets = wrappers.where((element) => element.visible).toList();
  if (visibleWidgets.isEmpty) return [];

  final List<Widget> items = [];
  for (int i = 0; i < visibleWidgets.length; i++) {
    items.add(visibleWidgets[i].builder(context));
    if (i < visibleWidgets.length - 1) {
      items.add(divider);
    }
  }
  return items;
}

class WidgetWrapper {
  final WidgetBuilder builder;
  final bool visible;

  const WidgetWrapper({
    required this.visible,
    required this.builder,
  });
}
